<template>
  <div>
    <c-table-box ref="cTableBox" hide-func checkbox :other-filters="otherFilters" :table-config="tableConfig" @selection-change="selectionChange">
      <template #btn>
        <template v-if="type==='all'">
          <Abnormal :sel-data="selections" @on-success="refreshTableData" />
          <ScoreDialog :sel-data="selections" @on-success="refreshTableData" />
        </template>
        <c-permission v-else-if="type==='decorateTask'" code="accept">
          <HandleTask :sel-data="selections" opt-code="deal" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='approach'" code="enter">
          <Approach :sel-data="selections" opt-code="enter" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='convoke'" code="kickoffmeeting">
          <Convoke :sel-data="selections" opt-code="kickoffmeeting" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='hide'" code="hiddenWorkAcceptance">
          <Hide :sel-data="selections" opt-code="hiddenWorkAcceptance" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='measure'" code="productResurvey">
          <Measure :sel-data="selections" opt-code="productResurvey" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='initialCheck'" code="initialSurvey">
          <InitialCheck :sel-data="selections" opt-code="initialSurvey" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='complete'" code="finished">
          <!-- <Complete :sel-data="selections" opt-code="finished" @on-success="refreshTableData" /> -->
          <c-button type="primary" opt-code="finished" @click="clickToRouterHandle('DecorateExecuteComplete', '装修完工')">装修完工</c-button>
        </c-permission>
      </template>
    </c-table-box>
  </div>
</template>
<script>
import { otherFiltersItem } from '@/utils'
import Abnormal from './abnormal'
import HandleTask from './handleTask'
import Approach from './approach'
import Convoke from './convoke'
import Hide from './hide'
import Measure from './measure'
import InitialCheck from './initialCheck'
// import Complete from './complete'
import ScoreDialog from './scoreDialog'
export default {
  name: 'List',
  components: {
    Abnormal,
    HandleTask,
    Approach,
    Convoke,
    Hide,
    Measure,
    InitialCheck,
    // Complete,
    ScoreDialog
  },
  props: {
    type: {
      type: String,
      default: ''
    },
    serachFilters: {
      type: Array,
      default: () => []
    },
    activeType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      tableConfig: {
        interfacePath: '/slm-admin/api/v1/shopDecoration/pageSelect',
        hideFuncBar: true,
        sort: 'createTime',
        order: 'desc',
        apiHeader: {
          'Permission-perCode': 'execute',
          'Permission-App': 'slm'
        },
        setting: [
          {
            prop: 'constCode',
            label: '门店建设项目号',
            width: 180
          },
          {
            prop: 'decorationTaskCode',
            label: '装修任务号',
            width: 200,
            className: 'btn-green',
            isButton: true,
            clickFun: this.toDetail
          },
          {
            prop: 'nature',
            label: '装修性质',
            width: 120,
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'DECORATION_NATURE'
            }
          },
          {
            prop: 'shopName',
            label: '所属门店',
            width: 120
          },
          {
            prop: 'clientName',
            label: '所属客户',
            width: 120
          },
          {
            prop: 'officeName',
            label: '所属办事处',
            width: 120
          },
          {
            prop: 'range',
            label: '装修范围',
            width: 120,
            render: (h, r) => {
              return h('c-dict-range-view', { props: { dictTypeCode: 'DECORATION_RANGE', value: r.range }})
            }

          },
          {
            prop: 'schedule',
            label: '装修进度',
            width: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'DECORATION_SCHEDULE'
            }
          },
          {
            prop: 'steps',
            label: '装修步骤',
            width: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'SHOP_DECORATION_STEPS'
            }
          },
          {
            prop: 'state',
            label: '装修状态',
            width: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'SHOP_DECORATION_STATE'
            }
          },
          {
            prop: 'isApplyReportOutTime',
            label: '装修申请是否超期',
            width: 140,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'BOOLEANS'
            }
          },
          {
            prop: 'isOutTimeUnaccept',
            label: '验收申请是否超期',
            width: 140,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'BOOLEANS'
            }
          },
          {
            prop: 'constUnitName',
            label: '施工单位',
            width: 120
          },
          {
            prop: 'principal',
            label: '施工负责人',
            width: 120
          },
          {
            prop: 'proSupervisorName',
            label: '工程监理',
            width: 120
          },
          {
            prop: 'marketType',
            label: '报图类型',
            width: 120,
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'MARKET_TYPE'
            }
          },
          {
            prop: 'constType',
            label: '门店建设类型',
            width: 120,
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'CONST_TYPE'
            }
          }
        ]
      },
      typeFilters: [],
      selections: []
    }
  },
  computed: {
    otherFilters() {
      return [...this.serachFilters, ...this.typeFilters]
    }
  },
  watch: {
    type: {
      handler(v) {
        this.setOtherFilters(v)
      },
      immediate: true
    },
    serachFilters: {
      handler(v) {
        if (this.activeType !== this.type) return
        this.$nextTick(() => {
          this.$refs.cTableBox.initTableData()
        })
      }
    }
  },
  created() {
  },
  activated() {
    if (this.ready && this.refreshTableData) {
      this.refreshTableData()
    }
  },
  deactivated() {
    this.ready = true
  },
  methods: {
    // 设置过滤条件
    setOtherFilters(v) {
      switch (v) {
        case 'all':
          this.typeFilters = []
          break
        case 'decorateTask':
          this.typeFilters = [
            otherFiltersItem('steps', 'C2', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'approach':
          this.typeFilters = [
            otherFiltersItem('steps', 'D1', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'convoke':
          this.typeFilters = [
            otherFiltersItem('steps', 'D2', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'hide':
          this.typeFilters = [
            otherFiltersItem('steps', 'E1', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'measure':
          this.typeFilters = [
            otherFiltersItem('steps', 'F1', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'initialCheck':
          this.typeFilters = [
            otherFiltersItem('steps', 'G1', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'complete':
          this.typeFilters = [
            otherFiltersItem('steps', 'H1', '='),
            // 除全部页签，其余全展示正常
            otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
      }
    },
    selectionChange(v) {
      this.selections = v
    },
    refreshTableData(v = 'onPage') {
      this.$nextTick(() => {
        this.$refs.cTableBox.initTableData(v)
      })
    },
    // 详情
    toDetail(row) {
      this.$router.push({ name: 'DecorateDetail', query: { id: row.id, taskCode: row.decorationTaskCode }})
    },
    // 跳转
    clickToRouterHandle(name, msg) {
      if (!this.singleJudge(this.selections, msg, true)) {
        return
      }
      const d = this.selections[0]
      this.$router.push({ name, query: { id: d.id, constCode: d.constCode, taskCode: d.taskCode, decorationTaskCode: d.decorationTaskCode }})
    }
  }
}
</script>
